
import {polygonFeatureStyle} from './../../common'
import yangtz from './../point/yangtz'
import river from './../point/river'
import Vue from 'vue'
import VectorSource from 'ol/source/vector'
import GeoJSON from 'ol/format/GeoJSON'
import {getTrueByProbability} from './../../../../utils/math'

/**
   * ###################
   * #######长江河道矢量图###
   * ###################
   */
const waterSystem = {
  noInteraction: true,
  // 名称
  name: '水系1',
  // 查询组件
  component: {},

  isquery: false,
  // 图层
  layers: {
    'changjiang-all': {
      name: '长江流域',
      isShow: false,
      /**
       * 事件回调函数
       * @param eventName 触发事件名称
       * @param param 传递参数
       */
      triggeCallback: function (eventName, isShow) {
        // 当事件为图层可见状态发生变化时
        if (eventName === 'layer-visible-change') {
          const {$vuexStore: vuex, $notify} = Vue.prototype
          if (isShow) {
            let riverLayer = vuex.getters.getLayerArray({options: {'interaction': ['river']}})
            let url = '/ssjc/yangzt/yangztLive?BASIN=1&isGeo_=true&typeKey_=gate&longitudeKey_=lng&latitudeKey_=lat'
            let vectorSource = new VectorSource({
              // 解析器
              format: new GeoJSON(),
              loader: function (extent, resolution, projection) {
                const {$getText} = Vue.prototype._vue
                $getText(url, {}).then(data => {
                  let f = vectorSource.getFormat()
                  // 解析器解析GeoJSON报文
                  let features = f.readFeatures(data, {featureProjection: projection})
                  f.readProjection(data)
                  f.getLastExtent()
                  vectorSource.addFeatures(features)
                }).catch(e => {
                  vectorSource.removeLoadedExtent(extent)
                })
              }
            })
            // 重设数据源
            riverLayer[0].setSource(vectorSource)
            vuex.commit('changeLayersOption',
              {
                group: 'interaction',
                layer: 'river',
                option: yangtz
              }
            )
            vuex.dispatch('changeMapPolygonLayersShow',
              {
                group: {
                  'interaction': ['river']
                },
                isShow: true
              }
            )
            // $notify({
            //   message: '已为您显示重庆至武汉段长江水情！', // 勾选长江流域切换为重庆至武汉段长江水情！
            //   type: 'success'
            // })
          } else {
            let riverLayer = vuex.getters.getLayerArray({options: {'interaction': ['river']}})
            let url = `/ssjc/River/RiverLive?name=''&isGeo_=true&typeKey_=river&longitudeKey_=lng&latitudeKey_=lat`
            let vectorSource = new VectorSource({
              // 解析器
              format: new GeoJSON(),
              loader: function (extent, resolution, projection) {
                const {$getText} = Vue.prototype._vue
                $getText(url, {}).then(data => {
                  let f = vectorSource.getFormat()
                  // 解析器解析GeoJSON报文
                  let features = f.readFeatures(data, {featureProjection: projection})
                  f.readProjection(data)
                  f.getLastExtent()
                  vectorSource.addFeatures(features)
                }).catch(e => {
                  vectorSource.removeLoadedExtent(extent)
                })
              }
            })
            // 重设数据源
            riverLayer[0].setSource(vectorSource)
            vuex.commit('changeLayersOption',
              {
                group: 'interaction',
                layer: 'river',
                option: river
              }
            )
            vuex.dispatch('changeMapPolygonLayersShow',
              {
                group: {
                  'interaction': ['river']
                },
                isShow: true
              }
            )
            // $notify({
            //   message: '已为您显示武汉河道水情！', // 取消长江流域勾选切换为武汉河道水情！
            //   type: 'success'
            // })
          }
        }
      },
      // 图例图标
      'icon-svg': '/static/icon/hedao-01.png',
      // 数据源
      src: {
        // 地址
        url: '/static/changjiang.json',
        // 参数
        param () {
          return {}
        }
      },
      // 图层样式
      featureStyle: function (feature, resolution) {
        return polygonFeatureStyle(getTrueByProbability(0.5) ? '长江' : '', 'rgba(0,255,255,0.6)', 'rgba(0,255,255,0)', feature, resolution)
      }
    }
  }
}
export default waterSystem
